package com.liveneo.adapter.mobile.web.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.google.common.collect.Lists;
import com.liveneo.adapter.base.entity.PrpmCaseInfo;
import com.liveneo.adapter.base.entity.PrpmLossConfirm;
import com.liveneo.adapter.base.entity.PrpmLossItem;
import com.liveneo.adapter.base.entity.PrpmLossRemark;
import com.liveneo.adapter.base.entity.PrpmOcrPayee;
import com.liveneo.adapter.base.entity.PrpmSurveyUser;
import com.liveneo.adapter.base.support.AdapterConstants;
import com.liveneo.adapter.base.support.BaseController;
import com.liveneo.adapter.base.support.LogUtils;
import com.liveneo.adapter.mobile.model.BaseResponse;
import com.liveneo.adapter.mobile.model.dto.AssessLossInfoDto;
import com.liveneo.adapter.mobile.model.dto.LossItemDto;
import com.liveneo.adapter.mobile.model.dto.LossRemarkDto;
import com.liveneo.adapter.mobile.model.dto.LossSingleDto;
import com.liveneo.adapter.mobile.model.dto.LossSingleSelfDto;
import com.liveneo.adapter.mobile.model.dto.MobileLossItemDto;
import com.liveneo.adapter.mobile.model.dto.PayeeDto;
import com.liveneo.adapter.mobile.model.dto.PlatLossSingleDto;
import com.liveneo.adapter.mobile.model.dto.PlatTurnsMobileDto;
import com.liveneo.adapter.mobile.model.dto.ReturnMsgDto;
import com.liveneo.adapter.mobile.model.request.AddLossItemRequest;
import com.liveneo.adapter.mobile.model.request.AssessLossInfoRequest;
import com.liveneo.adapter.mobile.model.request.AssessLossItemRequest;
import com.liveneo.adapter.mobile.model.request.CaseLossListRequest;
import com.liveneo.adapter.mobile.model.request.CaseUuidRequest;
import com.liveneo.adapter.mobile.model.request.LossConfirmRequest;
import com.liveneo.adapter.mobile.model.request.LossDetailRequest;
import com.liveneo.adapter.mobile.model.request.LossItemListRequest;
import com.liveneo.adapter.mobile.model.request.LossItemRequest;
import com.liveneo.adapter.mobile.model.request.LossReassignRequest;
import com.liveneo.adapter.mobile.model.request.LossRemarkRequest;
import com.liveneo.adapter.mobile.model.request.LossStatusRequest;
import com.liveneo.adapter.mobile.model.request.LossUuidRequest;
import com.liveneo.adapter.mobile.model.request.OcrPayeeInfoRequest;
import com.liveneo.adapter.mobile.model.request.SaveSurveyLossInfoRequest;
import com.liveneo.adapter.mobile.model.request.SurveyFinishRequest;
import com.liveneo.adapter.mobile.model.request.SurveyLossInfoRequest;
import com.liveneo.adapter.mobile.model.response.AssessLossInfoResponse;
import com.liveneo.adapter.mobile.model.response.CaseLossListResponse;
import com.liveneo.adapter.mobile.model.response.LossConfirmResponse;
import com.liveneo.adapter.mobile.model.response.LossDetailResponse;
import com.liveneo.adapter.mobile.model.response.LossInfoResponse;
import com.liveneo.adapter.mobile.model.response.LossItemListResponse;
import com.liveneo.adapter.mobile.model.response.LossItemResponse;
import com.liveneo.adapter.mobile.model.response.LossRemarkResponse;
import com.liveneo.adapter.mobile.model.response.OcrPayeeInfoResponse;
import com.liveneo.adapter.mobile.model.response.SurveyLossInfoResponse;
import com.liveneo.adapter.mobile.service.CaseInfoService;
import com.liveneo.adapter.mobile.service.LossConfirmService;
import com.liveneo.adapter.mobile.service.LossItemService;
import com.liveneo.adapter.mobile.service.MobileLossService;
import com.liveneo.adapter.mobile.service.MobileSaveModelFromClaimService;
import com.liveneo.adapter.mobile.service.PrpmSurveyUserService;
import com.liveneo.adapter.mobile.service.bussniess.MobileBussService;
import com.liveneo.adapter.mobile.support.MobileReturnCodeSupport;
import com.liveneo.adapter.mobile.util.Constant;
import com.liveneo.adapter.mobile.util.SysConst;
import com.liveneo.adapter.mobile.util.UUIDUtils;
import com.liveneo.adapter.outinterface.util.DateUtil;
import com.liveneo.adapter.outinterface.util.ResultInfo;
import com.liveneo.adapter.outinterface.web.platform.ClaimInterface;
import com.liveneo.system.base.utils.AppException;
import com.liveneo.system.base.utils.DateUtils;
import com.liveneo.system.base.utils.EncryptUtil;
import com.liveneo.system.base.utils.JsonUtils;
import com.liveneo.system.base.utils.TransModelUtil;

/**
 * 移动端定损相关接口
 * 
 * @author wangxx
 * 
 * @since 2016-06-12
 */
@Controller
@RequestMapping("/mobile")
public class MobileLossController extends BaseController {
    @Autowired
    private MobileLossService               mobileLossService;
    // @Autowired
    // private MobileImageService mobileImageService;
    @Autowired
    private CaseInfoService                 caseInfoService;
    @Autowired
    private LossItemService                 lossItemService;
    // @Autowired
    // private RequestClaimService requestClaimService;
    @Autowired
    private MobileSaveModelFromClaimService mobileSaveModelFromClaimService;
    @Autowired
    private LossConfirmService              lossConfirmService;
    @Autowired
    private PrpmSurveyUserService           prpmSurveyUserService;
    @Autowired
    private MobileBussService               mobileBussService;

    /**
     * MP44
     * 根据案件号和lossid查询定损单信息
     * 
     * @Title：定损单信息分享
     * @author zhangyw 2016年10月14日 上午10:33:45
     * @param data
     * @return String
     */
    @RequestMapping("getLossDetail.action")
    @ResponseBody
    public String getLossDetail(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "MP44入参=" + loguuid;
        LossDetailRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, LossDetailRequest.class);
        LossDetailResponse response = null;
        String result = "";
        String registNo = request.getRegistNo();
        String lossId = request.getLossId();
        try {
            PrpmLossItem prpmLossItem = lossItemService.getLossItemByCaseNoLossId(registNo, lossId);
            LossSingleDto lossSingleDto = new LossSingleDto();// 手机dto
            PlatLossSingleDto platLossSingleDto = new PlatLossSingleDto();// 平台dto
            // 理赔开关
            if ("1".equals(SysConst.getProperty("CLAIM_SUBMIT_FLAG"))) {
                // platLossSingleDto = requestClaimService.requestSingleLoss(registNo, lossId, prpmLossItem.getUuid(),
                // loguuid);
            }
            if (!"".equals(platLossSingleDto) && platLossSingleDto != null) {
                if (platLossSingleDto.getLossMain() != null && !"".equals(platLossSingleDto.getLossMain())) {
                    if (platLossSingleDto.getLossMain().getCaseNo() != null && !"".equals(platLossSingleDto.getLossMain().getCaseNo())) {
                        mobileSaveModelFromClaimService.saveLossSingleDto(platLossSingleDto, prpmLossItem.getLossItemId());
                        lossSingleDto = PlatTurnsMobileDto.turnsLossSingleDto(platLossSingleDto);
                    }
                }
            }
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null,
                    LossDetailResponse.class);
            response.setLossSingleDto(lossSingleDto);
        } catch (Exception e) {
            LogUtils.error("MP44--定损单信息分享--失败，", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, e.getMessage(),
                    LossDetailResponse.class);
        } finally {
            result = JsonUtils.toJson(response);
            // 日志记录响应json
            if (MobileReturnCodeSupport.CODE_SUCCESS.equals(response.getResultCode())) {
                LogUtils.info("MP44--定损单信息分享--返回结果【" + result + "】");
            } else {
                LogUtils.error("MP44--定损单信息分享--返回结果【" + result + "】");
            }
            result = EncryptUtil.encodeBase64(result);
        }
        return result;
    }

    /**
     * 获取定损任务状态
     * 
     */
    @RequestMapping("getLossStatus.action")
    @ResponseBody
    public String getLossStatus(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "getLossStatus入参=" + loguuid;
        LossStatusRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, LossStatusRequest.class);
        LossItemResponse response = null;
        String result = "";
        try {
            String uuids = request.getLossUuids();
            if (uuids == null) {
                // UUID为空
                response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_NOVALID_PARAM, MobileReturnCodeSupport.SYS_CODE,
                        MobileReturnCodeSupport.CODE_PARAM_NO_UUID, LossItemResponse.class);
            } else {
                // 参数不为空
                List<String> lossUuids = Arrays.asList(uuids.split(","));
                List<PrpmLossItem> lossItems = mobileLossService.getLossItemListByUuids(lossUuids);
                // 参数转化
                List<LossItemDto> dtos = Lists.newArrayList();
                for (PrpmLossItem item : lossItems) {
                    LossItemDto dto = new LossItemDto();
                    dto.setLossUuid(item.getLossItemId());
                    dto.setLossStatus(item.getLossStatus().toString());
                    dtos.add(dto);
                }
                // 返回结果
                response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null,
                        LossItemResponse.class);
                response.setLossItemList(dtos);
            }
        } catch (Exception e) {
            LogUtils.error("获取定损任务状态时出错，错误信息:", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null,
                    LossItemResponse.class);
        } finally {
            result = decorateResult(response);
        }
        return result;
    }

    /**
     * MP61
     * 1.点击定损单中心时调用，获取定损单案件列表；
     * 2.平台会调用理赔接口。
     * 
     * @Title：获取定损单案件列表
     * @author zhangyw 2016年10月14日 下午2:53:10
     * @param data
     * @return json
     */
    @RequestMapping("getCaseList.action")
    @ResponseBody
    public String getCaseList(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "MP61入参=" + loguuid;
        LossItemListRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, LossItemListRequest.class);
        LossItemListResponse response = new LossItemListResponse();
        request.setLogUuid(loguuid);
        String result = "";
        try {
            response = mobileBussService.getCaseList(request);
        } catch (Exception e) {
            LogUtils.error("MP61获取定损单案件列表->logUuid:获取失败，", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null,
                    LossItemListResponse.class);
        } finally {
            result = JsonUtils.toJson(response);
            // 日志记录响应json
            if (MobileReturnCodeSupport.CODE_SUCCESS.equals(response.getResultCode())) {
                LogUtils.info("MP61获取定损单案件列表->logUuid:返回结果【" + result + "】");
            } else {
                LogUtils.error("MP61获取定损单案件列表->logUuid:返回结果【" + result + "】");
            }
            result = EncryptUtil.encodeBase64(result);
        }
        return result;
    }

    /**
     * @Description:MP13移动查勘：获取定损单列表
     * @author Liuzhipeng 2016年10月14日 下午5:17:35
     * @return
     * @throws Exception
     */
    @RequestMapping("getCaseLossList.action")
    @ResponseBody
    public String getCaseLossList(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "MP13入参=" + loguuid;
        CaseLossListResponse response = new CaseLossListResponse();
        String result = "";
        try {
            CaseLossListRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, CaseLossListRequest.class);
            String userCode = request.getUserId(); // 用户编码
            String userId = request.getUserId();
            PrpmSurveyUser surveyUser = null;
            String userType = "";
            if (StringUtils.isNotBlank(request.getProjectCode())) userType = request.getProjectCode();
            if ("A".equals(userType)) {
                surveyUser = prpmSurveyUserService.findUserByUserCodeAndType(userId, "2");
            }
            if ("B".equals(userType)) {
                surveyUser = prpmSurveyUserService.findUserByUserCodeAndType(userId, "1");
            }
            String caseNo = request.getCaseNo(); // 案件号
            String caseUuid = request.getCaseUuid(); // 案件唯一标识
            if (userCode != null && !"".equals(userCode)) {
                List<PrpmLossItem> prpmLossItemList = lossItemService.getLossItemByUserCode(request.getProjectCode(), userId, caseUuid);// 查询未确认过的损失项
                // List<String> caseUUidList = new ArrayList<String>();
                List<MobileLossItemDto> lossItemSurveyTypeList = new ArrayList<MobileLossItemDto>();
                // 取出caseUUid放入list,此时caseUUid为重复的
                if (prpmLossItemList != null && prpmLossItemList.size() > 0) {
                    PrpmCaseInfo caseInfo = caseInfoService.selectByPrimaryKey(caseUuid);// 获取caseUUid获取prpmcaseinfo
                    for (int j = 0; j < prpmLossItemList.size(); j++) {
                        MobileLossItemDto prpmLossItemDto = new MobileLossItemDto();
                        PrpmLossItem prpmLossItem = prpmLossItemList.get(j);
                        prpmLossItemDto.setCaseNo(prpmLossItem.getCaseNo());
                        prpmLossItemDto.setCaseUuid(prpmLossItem.getCaseNo());
                        prpmLossItemDto.setLossType(prpmLossItem.getLossType() + "");
                        prpmLossItemDto.setLossId(prpmLossItem.getLossId().toString());
                        prpmLossItemDto.setObjectName(StringUtils.isBlank(prpmLossItem.getLossName()) ? "" : prpmLossItem.getLossName());
                        // prpmLossItemDto.setOwerName(prpmLossItem.getContact());
                        prpmLossItemDto.setLossUuid(prpmLossItem.getLossItemId());
                        prpmLossItemDto.setDamageDate(caseInfo.getDamageDate() != null ? DateUtil.formatDate(caseInfo.getDamageDate()) : "");
                        prpmLossItemDto.setDamagePerson(prpmLossItem.getContact());
                        prpmLossItemDto.setFlag("");
                        // 定损单状态：0未完成 1-定损完成待确认 2-客户拒绝（仅C端） 3-客户同意
                        // （仅C端）4-查勘员修理厂确认（AB端）5-后台确认
                        // 调用理赔核心接口开始
                        PlatLossSingleDto platLossSingleDto = new PlatLossSingleDto();// 平台dto
                        // 理赔开关
                        // if
                        // ("1".equals(SysConst.getProperty("CLAIM_SUBMIT_FLAG")))
                        // {
                        ResultInfo resultInfo = ClaimInterface.findEvaluateInfo(prpmLossItem.getCaseNo(), String.valueOf(prpmLossItem.getLossId()));
                        if (resultInfo.getEvaluateInfoResponseDto() != null) {
                            platLossSingleDto = (new TransModelUtil()).transToPlatLossSingleDto(resultInfo.getEvaluateInfoResponseDto(),
                                    String.valueOf(prpmLossItem.getLossId()), prpmLossItem.getLossItemId());
                            // 有定损单信息时
                            if (prpmLossItem.getLossitemStatus() != null && prpmLossItem.getLossitemStatus() != 0) {// 非0情况显示为数据库状态
                                prpmLossItemDto.setLossStatus(prpmLossItem.getLossitemStatus() + "");
                            } else {
                                prpmLossItemDto.setLossStatus("1");// 0或者空情况显示为1-定损完成待确认
                                // todo
                                PrpmLossItem prpmLossItemUpdate = new PrpmLossItem();
                                prpmLossItemUpdate.setLossItemId(prpmLossItem.getLossItemId());
                                prpmLossItemUpdate.setLossitemStatus(1);
                                prpmLossItemUpdate.setModifyBy(surveyUser != null ? surveyUser.getSurveyName() : request.getUserId());
                                prpmLossItemUpdate.setModifyTime(new Date());
                                lossItemService.updateByPrimaryKeySelective(prpmLossItemUpdate);
                            }
                        } else {// 无定损单信息时
                            prpmLossItemDto.setLossStatus("0");// 0未完成
                        }
                        // }
                        LossSingleDto lossSingleDto = new LossSingleDto();
                        if (!"".equals(platLossSingleDto) && platLossSingleDto != null) {
                            if (platLossSingleDto.getLossMain() != null && !"".equals(platLossSingleDto.getLossMain())) {
                                if (platLossSingleDto.getLossMain().getCaseNo() != null && !"".equals(platLossSingleDto.getLossMain().getCaseNo())) {
                                    mobileSaveModelFromClaimService.saveLossSingleDto(platLossSingleDto, prpmLossItem.getLossItemId());
                                    lossSingleDto = PlatTurnsMobileDto.turnsLossSingleDto(platLossSingleDto);
                                }
                            }
                        }
                        // 调用理赔核心接口结束
                        prpmLossItemDto.setLossSingleDto(lossSingleDto);
                        lossItemSurveyTypeList.add(prpmLossItemDto);
                        // }
                    }
                }
                /* -封装损失信息对象end - */
                response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, "",
                        CaseLossListResponse.class);
                response.setCaseNo(caseNo);
                response.setLossItemSurveyTypeList(lossItemSurveyTypeList);
            } else {
                throw new Exception("用户编码为空！");
            }
        } catch (Exception e) {
            LogUtils.error("获取案件下定损列表失败，", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, e.getMessage(),
                    CaseLossListResponse.class);
        } finally {
            result = JsonUtils.toJson(response);
            // 日志记录响应json
            if (MobileReturnCodeSupport.CODE_SUCCESS.equals(response.getResultCode())) {
                LogUtils.info("MP13返回结果【" + result + "】");
            } else {
                LogUtils.error("MP13返回结果【" + result + "】");
            }
            result = EncryptUtil.encodeBase64(result);
        }
        return result;
    }

    /**
     * MP14
     * @Description:移动查勘：获取定损单详情
     * @return null
     * @throws Exception
     * @ReturnType:String
     * @exception
     * @Since:2016-9-28 上午10:30:43
     * @Author:liuzhipeng
     */
    @RequestMapping("getSingleLoss.action")
    @ResponseBody
    public String getSingleLoss(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "MP14入参=" + loguuid;
        String result = "";
        LossItemRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, LossItemRequest.class);
        LossItemResponse response = new LossItemResponse();
        String registNo = request.getRegistNo();
        String lossId = request.getLossId();
        try {
            PrpmLossItem prpmLossItem = lossItemService.getLossItemByCaseNoLossId(registNo, lossId);
            LossSingleDto lossSingleDto = new LossSingleDto();// 手机dto
            PlatLossSingleDto platLossSingleDto = new PlatLossSingleDto();// 平台dto
            // 理赔开关
            if ("1".equals(SysConst.getProperty("CLAIM_SUBMIT_FLAG"))) {
                // platLossSingleDto = requestClaimService.requestSingleLoss(registNo, lossId, prpmLossItem.getUuid(),
                // loguuid);
            }
            if (!"".equals(platLossSingleDto) && platLossSingleDto != null) {
                if (platLossSingleDto.getLossMain() != null && !"".equals(platLossSingleDto.getLossMain())) {
                    if (platLossSingleDto.getLossMain().getCaseNo() != null && !"".equals(platLossSingleDto.getLossMain().getCaseNo())) {
                        mobileSaveModelFromClaimService.saveLossSingleDto(platLossSingleDto, prpmLossItem.getLossItemId());
                        lossSingleDto = PlatTurnsMobileDto.turnsLossSingleDto(platLossSingleDto);
                    }
                }
            }
            response.setLossSingleDto(lossSingleDto);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null,
                    LossItemResponse.class);
        } catch (Exception e) {
            LogUtils.error("获取案件下定损列表失败，", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, e.getMessage(),
                    LossItemResponse.class);
        } finally {
            result = JsonUtils.toJson(response);
            // 日志记录响应json
            if (MobileReturnCodeSupport.CODE_SUCCESS.equals(response.getResultCode())) {
                LogUtils.info("MP14返回结果【" + result + "】");
            } else {
                LogUtils.error("MP14返回结果【" + result + "】");
            }
            result = EncryptUtil.encodeBase64(result);
        }
        return result;
    }

    /***
     * 移动查勘：定损单确认接口
     * MP17
     * 
     * @return
     * @throws Exception
     */
    @RequestMapping("caseStatusConfirm.action")
    @ResponseBody
    public String caseStatusConfirm(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "MP17入参=" + loguuid;
        LossItemRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, LossItemRequest.class);
        String caseNo = request.getCaseNo();
        String lossId = request.getLossId();
        String lossUuid = request.getLossUuid();
        LossItemResponse response = new LossItemResponse();
        String result = "";
        try {
            if (StringUtils.isNotBlank(caseNo) && StringUtils.isNotBlank(lossId) && StringUtils.isNotBlank(lossUuid)) {
                PrpmLossItem prpmLossItem = lossItemService.getLossItemByCaseNoLossIdUuid(caseNo, lossId, lossUuid);
                if (prpmLossItem != null && !"".equals(prpmLossItem)) {
                    prpmLossItem.setLossitemStatus(4); // 查勘员修理厂确认（AB端）
                    lossItemService.updateLossItem(prpmLossItem);
                    PrpmLossConfirm prpmLossConfirm = new PrpmLossConfirm();
                    prpmLossConfirm.setCaseNo(caseNo);
                    prpmLossConfirm.setConfirmDate(new Date());
                    prpmLossConfirm.setConfirmType(1);
                    prpmLossConfirm.setMobile(prpmLossItem.getMobile());
                    prpmLossConfirm.setConfirmBy(prpmLossItem.getSurveyorCode());
                    prpmLossConfirm.setCaseInfoId(prpmLossItem.getCaseInfoId());
                    prpmLossConfirm.setLossItemId(prpmLossItem.getLossItemId());
                    prpmLossConfirm.setLossId(prpmLossItem.getLossId());
                    prpmLossConfirm.setLossitemStatus(4); // 4-查勘员修理厂确认（AB端）
                    PrpmLossConfirm prpmLossConfirmQuery = lossConfirmService.selectByLossItemId(prpmLossItem.getLossItemId());
                    if (prpmLossConfirmQuery != null) {
                        lossConfirmService.updateByPrimaryKeySelective(prpmLossConfirm);
                    } else {
                        prpmLossConfirm.setLossConfirmId(UUIDUtils.getUUIDStr());
                        lossConfirmService.insert(prpmLossConfirm);
                    }
                    response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null,
                            LossItemResponse.class);
                } else {
                    response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.KEY_MOBILE_00020, MobileReturnCodeSupport.SYS_CODE, null,
                            LossItemResponse.class);
                }
            } else {
                LogUtils.error("入参为空");
                response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_ERR_PARAM_NO_EXISTS, MobileReturnCodeSupport.SYS_CODE, null,
                        LossItemResponse.class);
            }
        } catch (Exception e) {
            LogUtils.error("获取案件下定损列表失败，", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, e.getMessage(),
                    LossItemResponse.class);
        } finally {
            result = JsonUtils.toJson(response);
            // 日志记录响应json
            if (MobileReturnCodeSupport.CODE_SUCCESS.equals(response.getResultCode())) {
                LogUtils.info("MP17返回结果【" + result + "】");
            } else {
                LogUtils.error("MP17返回结果【" + result + "】");
            }
            result = EncryptUtil.encodeBase64(result);
        }
        return result;
    }

    /**
     * 获取案件下定损任务
     * 
     */
    @RequestMapping("getLossItemList.action")
    @ResponseBody
    public String getLossItemList(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "getLossItemList入参=" + loguuid;
        LossItemRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, LossItemRequest.class);
        LossItemResponse response = null;
        String result = "";
        try {
            // 获取案件下任务列表
            List<PrpmLossItem> items = mobileLossService.getLossItemList(request);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null,
                    LossItemResponse.class);
            // 转化结果
            if (items.size() > 0) {
                String userCode = request.getSurveyUserCode();
                String caseNo = items.get(0).getCaseNo();
                response.setCaseNo(caseNo);
                List<LossItemDto> lossItemList = Lists.newArrayList();
                for (PrpmLossItem item : items) {
                    LossItemDto dto = new LossItemDto();
                    dto.setLossUuid(item.getLossItemId());
                    if (item.getLossType() != null) {
                        dto.setLossType(item.getLossType().toString());
                    }
                    dto.setLossId(String.valueOf(item.getLossId()));
                    dto.setLossStatus(item.getLossStatus().toString());
                    dto.setSurveyorCode(item.getSurveyorCode());
                    dto.setSurveyorName(item.getSurveyorName());
                    dto.setCreateTime(DateUtils.formatDate(item.getCreateTime()));
                    if (userCode.equals(item.getSurveyorCode())) {
                        dto.setOwn("1");
                    } else {
                        dto.setOwn("0");
                    }
                    lossItemList.add(dto);
                }
                response.setLossItemList(lossItemList);
            }
            response.setCaseUuid(request.getCaseUuid());
        } catch (AppException e) {
            LogUtils.error("获取案件下定损列表失败，", e);
            response = MobileReturnCodeSupport.buildReponse(e.getCode(), MobileReturnCodeSupport.SYS_CODE, e.getDetailCode(), LossItemResponse.class);
        } catch (Exception e) {
            LogUtils.error("获取案件下定损列表失败，", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null,
                    LossItemResponse.class);
        } finally {
            result = JsonUtils.toJson(response);
            // 日志记录响应json
            if (MobileReturnCodeSupport.CODE_SUCCESS.equals(response.getResultCode())) {
                LogUtils.info("getLossItemList返回结果【" + result + "】");
            } else {
                LogUtils.error("getLossItemList返回结果【" + result + "】");
            }
            result = EncryptUtil.encodeBase64(result);
        }
        return result;
    }

    /**
     * 获取定损任务
     */
    @RequestMapping("getAssessLossItem.action")
    @ResponseBody
    public String getAssessLossItem(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "getAssessLossItem入参=" + loguuid;
        AssessLossItemRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, AssessLossItemRequest.class);
        LossItemResponse response = null;
        String result = "";
        try {
            // 查询列表
            List<PrpmLossItem> items = mobileLossService.getAssessLossItem(request);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null,
                    LossItemResponse.class);
            // 转化结果
            if (items.size() > 0) {
                List<LossItemDto> lossItemList = Lists.newArrayList();
                for (PrpmLossItem item : items) {
                    LossItemDto dto = new LossItemDto();
                    dto.setCaseNo(item.getCaseNo());
                    dto.setUuid(item.getCaseNo());
                    dto.setLossUuid(item.getLossItemId());
                    dto.setLossName(item.getLossName());
                    if (item.getLossType() != null) {
                        dto.setLossType(item.getLossType().toString());
                    }
                    dto.setLossId(String.valueOf(item.getLossId()));
                    dto.setLossStatus(item.getLossStatus().toString());
                    // TODO 待数据库中增加 查勘员确认状态
                    // dto.setFlag(item.getFlag());
                    lossItemList.add(dto);
                }
                response.setLossItemList(lossItemList);
            }
        } catch (AppException e) {
            LogUtils.error("获取定损列表失败，", e);
            response = MobileReturnCodeSupport.buildReponse(e.getCode(), MobileReturnCodeSupport.SYS_CODE, e.getDetailCode(), LossItemResponse.class);
        } catch (Exception e) {
            LogUtils.error("获取定损列表失败，", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null,
                    LossItemResponse.class);
        } finally {
            result = JsonUtils.toJson(response);
            // 日志记录响应json
            if (MobileReturnCodeSupport.CODE_SUCCESS.equals(response.getResultCode())) {
                LogUtils.info("返回结果【" + result + "】");
            } else {
                LogUtils.error("返回结果【" + result + "】");
            }
            result = EncryptUtil.encodeBase64(result);
        }
        return result;
    }

    /**
     * @Title:获取定损单详情
     * @Description: TODO
     * @param @param data
     * @param @return 设定文件
     * @return String 返回类型
     * @throws
     */
    @SuppressWarnings("unused")
    @RequestMapping("/getAssessLossInfo.action")
    @ResponseBody
    public String getAssessLossInfo(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "getAssessLossInfo入参=" + loguuid;
        AssessLossInfoResponse response = null;
        String result = null;
        try {
            AssessLossInfoRequest ar = JsonUtils.fromJsonWithBase64(logTitle, data, AssessLossInfoRequest.class);
            PrpmLossItem prpmLossItem = mobileLossService.getLossItemByCaseNoLossId(ar.getCaseNo(), ar.getLossId());
            // 调用核心
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null,
                    AssessLossInfoResponse.class);
        } catch (Exception e) {
            LogUtils.error("获取定损单详情系统异常,错误信息：", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null,
                    AssessLossInfoResponse.class);
        }
        result = JsonUtils.toJson(response);
        LogUtils.info("获取定损单详情返回结果：" + result);
        // 加密返回
        result = EncryptUtil.encodeBase64(result);
        return result;
    }

    /**
     * @Title: 定损单确认接口
     * @Description:
     * @param @param data
     * @param @return String
     * @return String 返回类型
     * @throws
     */
    @SuppressWarnings("unused")
    @RequestMapping("/lossConfirm.action")
    @ResponseBody
    public String lossConfirm(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "lossConfirm入参=" + loguuid;
        String result = null; // 结果
        BaseResponse response = null;
        try {
            LossConfirmRequest lc = JsonUtils.fromJsonWithBase64(logTitle, data, LossConfirmRequest.class);
            PrpmLossItem prpmLossItem = mobileLossService.getLossItemByLossUuid(lc.getLossUuid());
            // 调核心
            response = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_ERR_PARAM_NO_EXISTS, MobileReturnCodeSupport.SYS_CODE, null);
        } catch (Exception e) {
            LogUtils.error(" 定损单确认接口系统异常,错误信息：", e);
            response = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null);
        }
        result = JsonUtils.toJson(response);
        LogUtils.info("定损单确认接口返回结果：" + result);
        // 加密返回
        result = EncryptUtil.encodeBase64(result);
        return result;
    }

    /**
     * @Title: 查询定损单状态
     * @Description:
     * @param @param data
     * @param @return 设定文件
     * @return String 返回类型
     * @throws
     */
    @RequestMapping("/lossConfirmStatus.action")
    @ResponseBody
    public String lossConfirmStatus(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "lossConfirmStatus入参=" + loguuid;
        String result = null;// 结果
        LossConfirmResponse response = null;
        try {
            LossConfirmRequest lc = JsonUtils.fromJsonWithBase64(logTitle, data, LossConfirmRequest.class);
            // 查询定损任务信息
            PrpmLossItem prpmLossItem = mobileLossService.getLossItemByLossUuid(lc.getLossUuid());
            if (prpmLossItem != null) {
                response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null,
                        LossConfirmResponse.class);
                response.setLossStatus(prpmLossItem.getLossStatus().toString());
            } else {
                // 错误
                response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_LOSSUUID_NO_ERROR, MobileReturnCodeSupport.SYS_CODE, null,
                        LossConfirmResponse.class);
            }
        } catch (Exception e) {
            LogUtils.error("查询定损单状态系统异常,错误信息：", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null,
                    LossConfirmResponse.class);
        }
        // 结果封装
        result = JsonUtils.toJson(response);
        LogUtils.info("查询定损单状态返回结果：" + result);
        // 加密返回
        result = EncryptUtil.encodeBase64(result);
        return result;
    }

    /**
     * MP20 @Title: 图片查勘流转接口(生成查勘案件) @Description: @param @param
     * data @param @return 设定文件 @return String 返回类型 @throws
     */
    @RequestMapping("/surveyFinish.action")
    @ResponseBody
    public String surveyFinish(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "MP20入参=" + loguuid;
        String result = null; // 结果
        BaseResponse response = null;
        SurveyFinishRequest sr = JsonUtils.fromJsonWithBase64(logTitle, data, SurveyFinishRequest.class);
        sr.setLogUuid(loguuid);
        try {
            response = mobileBussService.surveyFinish(sr);
        } catch (Exception e) {
            LogUtils.error("MP20->logUuid:" + loguuid + "图片查勘流转接口系统异常,错误信息：", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null,
                    BaseResponse.class);
        } finally {
            // 结果封装
            result = JsonUtils.toJson(response);
            LogUtils.info("MP20->logUuid:" + loguuid + "图片查勘流转接口返回参数：" + result);
            // 加密返回
            result = EncryptUtil.encodeBase64(result);
        }
        return result;
    }

    /**
     * 获取查勘信息列表
     */
    // @RequestMapping("getSurveyLossItem.action")
    // @ResponseBody
    // public String getSurveyLossItem(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data)
    // throws AppException {
    // CaseUuidRequest request = JsonUtils.fromJsonWithBase64(data, CaseUuidRequest.class);
    // SurveyLossItemResponse response = null;
    // String result = "";
    // try {
    // String caseUuid = request.getCaseUuid();
    // if (caseUuid == null || "".equals(caseUuid)) {
    // // 案件ID为空
    // response =
    // MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_NOVALID_PARAM,MobileReturnCodeSupport.SYS_CODE,
    // MobileReturnCodeSupport.CODE_PARAM_NO_UUID, SurveyLossItemResponse.class);
    // } else {
    // // 参数不为空
    // List<PrpmLossItem> items = mobileLossService.getSurveyLossItem(caseUuid);
    // // 转化结果
    // List<SurveyItemDto> dtos = Lists.newArrayList();
    // for (PrpmLossItem item : items) {
    // SurveyItemDto dto = new SurveyItemDto();
    // Integer lossType = item.getLossType();
    // if (lossType != null) {
    // dto.setSurveyItemFlag(lossType.toString());
    // }
    // dto.setLossName(item.getLossName());
    // dto.setLossUuid(item.getLossItemId());
    // List<String> usable = Lists.newArrayList("0", "1", "2", "6", "7");
    // if (usable.contains(lossType.toString())) {
    // dto.setUsable("1");
    // } else {
    // dto.setUsable("0");
    // }
    // //dto.setEntry(item.getFlag());
    // dtos.add(dto);
    // }
    // response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS,
    // MobileReturnCodeSupport.SYS_CODE,null,
    // SurveyLossItemResponse.class);
    // response.setSurveyItemList(dtos);
    // }
    // } catch (Exception e) {
    // LogUtils.error("获取查勘信息列表失败，", e);
    // response =
    // MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR,MobileReturnCodeSupport.SYS_CODE,
    // null,
    // SurveyLossItemResponse.class);
    // } finally {
    // result = JsonUtils.toJson(response);
    // // 日志记录响应json
    // if (MobileReturnCodeSupport.CODE_SUCCESS.equals(response.getResultCode())) {
    // LogUtils.info("返回结果【" + result + "】");
    // } else {
    // LogUtils.error("返回结果【" + result + "】");
    // }
    // result = EncryptUtil.encodeBase64(result);
    // }
    // return result;
    // }
    /**
     * 获取查勘信息列表
     */
    @RequestMapping("getSurveyLossInfo.action")
    @ResponseBody
    public String getSurveyLossInfo(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "getSurveyLossInfo入参=" + loguuid;
        SurveyLossInfoRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, SurveyLossInfoRequest.class);
        SurveyLossInfoResponse response = null;
        String result = "";
        try {
            response = mobileLossService.getSurveyLossInfo(request.getLossUuid(), request.getSurveyItemFlag());
        } catch (AppException e) {
            LogUtils.error("获取查勘信息详情失败，", e);
            response = MobileReturnCodeSupport.buildReponse(e.getCode(), MobileReturnCodeSupport.SYS_CODE, e.getDetailCode(), SurveyLossInfoResponse.class);
        } catch (Exception e) {
            LogUtils.error("获取查勘信息详情失败，", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null,
                    SurveyLossInfoResponse.class);
        } finally {
            result = JsonUtils.toJson(response);
            // 日志记录响应json
            if (MobileReturnCodeSupport.CODE_SUCCESS.equals(response.getResultCode())) {
                LogUtils.info("返回结果【" + result + "】");
            } else {
                LogUtils.error("返回结果【" + result + "】");
            }
            result = EncryptUtil.encodeBase64(result);
        }
        return result;
    }

    /**
     * 查勘信息 暂存
     */
    @RequestMapping("saveSurveyLossInfo.action")
    @ResponseBody
    public String saveSurveyLossInfo(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "saveSurveyLossInfo入参=" + loguuid;
        SaveSurveyLossInfoRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, SaveSurveyLossInfoRequest.class);
        BaseResponse response = null;
        String result = "";
        try {
            mobileLossService.saveSurveyLossInfo(request);
            response = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null);
        } catch (Exception e) {
            LogUtils.error("查勘信息暂存失败，", e);
            response = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null);
        } finally {
            result = decorateResult(response);
        }
        return result;
    }

    /**
     * 查勘信息 提交
     */
    @SuppressWarnings("unused")
    @RequestMapping("submitSurveyInfo.action")
    @ResponseBody
    public String submitSurveyInfo(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "submitSurveyInfo入参=" + loguuid;
        CaseUuidRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, CaseUuidRequest.class);
        BaseResponse response = null;
        String result = "";
        try {
            response = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null);
        } catch (Exception e) {
            LogUtils.error("查勘信息提交失败，", e);
            response = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null);
        } finally {
            result = decorateResult(response);
        }
        return result;
    }

    /**
     * 增加领款人信息
     */
    @RequestMapping("addPayeeInfo.action")
    @ResponseBody
    public String addPayeeInfo(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "addPayeeInfo入参=" + loguuid;
        OcrPayeeInfoRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, OcrPayeeInfoRequest.class);
        BaseResponse response = null;
        String result = "";
        try {
            // 增加领款人信息
            mobileLossService.addPayeeInfo(request);
            // 返回成功
            response = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null);
        } catch (AppException e) {
            LogUtils.error("增加领款人信息失败，", e);
            response = MobileReturnCodeSupport.buildBaseReponse(e.getCode(), MobileReturnCodeSupport.SYS_CODE, e.getDetailCode());
        } catch (Exception e) {
            LogUtils.error("增加领款人信息失败，", e);
            response = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null);
        } finally {
            result = decorateResult(response);
        }
        return result;
    }

    /**
     * 获取领款人信息
     */
    @RequestMapping("getPayeeInfo.action")
    @ResponseBody
    public String getPayeeInfo(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "getPayeeInfo入参=" + loguuid;
        LossUuidRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, LossUuidRequest.class);
        OcrPayeeInfoResponse response = null;
        String result = "";
        try {
            String lossUuid = request.getLossUuid();
            PrpmOcrPayee payeeInfo = mobileLossService.getPayeeInfo(lossUuid);
            PayeeDto dto = new PayeeDto();
            BeanUtils.copyProperties(dto, payeeInfo);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null,
                    OcrPayeeInfoResponse.class);
        } catch (Exception e) {
            LogUtils.error("获取领款人信息失败，", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null,
                    OcrPayeeInfoResponse.class);
        } finally {
            result = decorateResult(response);
        }
        return result;
    }

    /**
     * 
     * 增加备注信息
     */
    @RequestMapping("addLossRemark.action")
    @ResponseBody
    public String addLossRemark(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "addLossRemark入参=" + loguuid;
        LossRemarkRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, LossRemarkRequest.class);
        BaseResponse response = null;
        String result = "";
        try {
            mobileLossService.addLossRemark(request);
            response = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null);
        } catch (AppException e) {
            LogUtils.error("增加备注信息失败，", e);
            response = MobileReturnCodeSupport.buildBaseReponse(e.getCode(), MobileReturnCodeSupport.SYS_CODE, e.getDetailCode());
        } catch (Exception e) {
            LogUtils.error("增加备注信息失败，", e);
            response = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null);
        } finally {
            result = decorateResult(response);
        }
        return result;
    }

    /**
     * 获取查勘任务备注信息
     */
    @RequestMapping("getLossRemark.action")
    @ResponseBody
    public String getLossRemark(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "getLossRemark入参=" + loguuid;
        LossUuidRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, LossUuidRequest.class);
        LossRemarkResponse response = null;
        String result = "";
        try {
            String lossUuid = request.getLossUuid();
            List<PrpmLossRemark> lossRemarks = mobileLossService.getLossRemarkByLossUuid(lossUuid);
            List<LossRemarkDto> dtos = Lists.newArrayList();
            for (PrpmLossRemark lossRemark : lossRemarks) {
                LossRemarkDto dto = new LossRemarkDto();
                dto.setCaseNo(lossRemark.getCaseNo());
                dto.setLossUuid(lossRemark.getLossUuid());
                dto.setOrgCode(lossRemark.getOrgCode());
                dto.setSurveyUserCode(lossRemark.getSurveyorCode());
                dto.setSurveyUserName(lossRemark.getSurveyorName());
                if (lossRemark.getCreateTime() != null) {
                    dto.setCreateTime(DateUtils.formatDateTime(lossRemark.getCreateTime()));
                }
                dto.setRemark(lossRemark.getRemark());
                dtos.add(dto);
            }
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null,
                    LossRemarkResponse.class);
            response.setLossRemarkDto(dtos);
        } catch (Exception e) {
            LogUtils.error("获取查勘备注信息失败，", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null,
                    LossRemarkResponse.class);
        } finally {
            result = decorateResult(response);
        }
        return result;
    }

    /**
     * 增加损失任务
     */
    @RequestMapping("addLossItem.action")
    @ResponseBody
    public String addLossItem(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "addLossItem入参=" + loguuid;
        AddLossItemRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, AddLossItemRequest.class);
        BaseResponse response = null;
        String result = "";
        try {
            mobileLossService.addLossItem(request);
            response = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null);
        } catch (AppException e) {
            LogUtils.error("增加损失任务失败，", e);
            response = MobileReturnCodeSupport.buildBaseReponse(e.getCode(), MobileReturnCodeSupport.SYS_CODE, e.getDetailCode());
        } catch (Exception e) {
            LogUtils.error("增加损失任务失败，", e);
            response = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null);
        } finally {
            result = decorateResult(response);
        }
        return result;
    }

    /**
     * 获取定损信息
     */
    @RequestMapping("getLossInfo.action")
    @ResponseBody
    public String getLossInfo(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "getLossInfo入参=" + loguuid;
        LossUuidRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, LossUuidRequest.class);
        LossInfoResponse response = null;
        String result = "";
        try {
            String lossUuid = request.getLossUuid();
            AssessLossInfoDto dto = mobileLossService.getLossInfo(lossUuid);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null,
                    LossInfoResponse.class);
            response.setAssessLossInfoDto(dto);
        } catch (Exception e) {
            LogUtils.error("获取定损信息出错", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null,
                    LossInfoResponse.class);
        } finally {
            result = decorateResult(response);
        }
        return result;
    }

    /**
     * 申请改派
     */
    @RequestMapping("lossReassign.action")
    @ResponseBody
    public String lossReassign(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "lossReassign入参=" + loguuid;
        LossReassignRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, LossReassignRequest.class);
        BaseResponse response = null;
        String result = "";
        try {
            mobileLossService.lossReassign(request);
            response = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null);
        } catch (Exception e) {
            LogUtils.error("增加损失任务失败，", e);
            response = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null);
        } finally {
            result = decorateResult(response);
        }
        return result;
    }

    /**
     * 
     * @Description:自助：获取定损单详情
     * @return
     * @ReturnType:
     * @exception
     * @Since:2016-9-28 下午03:05:56
     * @Author:liuzhipeng
     */
    @RequestMapping("getSelfSingleLoss.action")
    @ResponseBody
    public String getSelfSingleLoss(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "getSelfSingleLoss入参=" + loguuid;
        LossItemRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, LossItemRequest.class);
        LossItemResponse response = new LossItemResponse();
        String result = "";
        String registNo = request.getCaseNo();
        try {
            PrpmCaseInfo prpmCaseInfo = caseInfoService.getCaseInfo(registNo);
            if (prpmCaseInfo == null || "".equals(prpmCaseInfo)) {
                throw new Exception("案件不存在");
            } else {
                // 根据报案号查案件损失信息表
                // 没有做lossid限制
                List<PrpmLossItem> prpmLossItemList = lossItemService.getLossItemByCaseNo(registNo);
                if (prpmLossItemList != null && prpmLossItemList.size() > 0) {
                    List<LossSingleSelfDto> lossSingleSelfDtoList = new ArrayList<LossSingleSelfDto>();
                    for (int i = 0; i < prpmLossItemList.size(); i++) {
                        PrpmLossItem loss = prpmLossItemList.get(i);
                        int lossType = loss.getLossType();
                        if (lossType != 3 && lossType != 7) {
                            LossSingleSelfDto lossSingleSelfDto = new LossSingleSelfDto();
                            LossSingleDto lossSingleDto = new LossSingleDto();
                            PlatLossSingleDto platLossSingleDto = new PlatLossSingleDto();
                            // 理赔开关
                            if ("1".equals(SysConst.getProperty("CLAIM_SUBMIT_FLAG"))) {
                                ResultInfo resultInfo = ClaimInterface.findEvaluateInfo(registNo, String.valueOf(prpmLossItemList.get(i).getLossId()));
                                if (resultInfo.getEvaluateInfoResponseDto() != null) {
                                    platLossSingleDto = (new TransModelUtil()).transToPlatLossSingleDto(resultInfo.getEvaluateInfoResponseDto(),
                                            String.valueOf(prpmLossItemList.get(i).getLossId()), prpmLossItemList.get(i).getLossItemId());
                                }
                            }
                            if (!"".equals(platLossSingleDto) && platLossSingleDto != null) {
                                if (platLossSingleDto.getLossMain() != null && !"".equals(platLossSingleDto.getLossMain())) {
                                    if (platLossSingleDto.getLossMain().getCaseNo() != null && !"".equals(platLossSingleDto.getLossMain().getCaseNo())) {
                                        mobileSaveModelFromClaimService.saveLossSingleDto(platLossSingleDto, prpmLossItemList.get(i).getLossItemId());
                                        lossSingleDto = PlatTurnsMobileDto.turnsLossSingleDto(platLossSingleDto);
                                        MobileLossItemDto mobileLossItemDto = new MobileLossItemDto();
                                        BeanUtils.copyProperties(mobileLossItemDto, prpmLossItemList.get(i));
                                        mobileLossItemDto.setLossUuid(prpmLossItemList.get(i).getLossItemId());
                                        lossSingleSelfDto.setLossSingleDto(lossSingleDto);
                                        lossSingleSelfDto.setPrpmLossItem(mobileLossItemDto);
                                        lossSingleSelfDtoList.add(lossSingleSelfDto);
                                    }
                                }
                            }
                        }
                    }
                    response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null,
                            LossItemResponse.class);
                    response.setLossSingleSelfDtoList(lossSingleSelfDtoList);
                } else {
                    response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null,
                            LossItemResponse.class);
                }
            }
        } catch (Exception e) {
            LogUtils.error("获取案件下定损列表失败，", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, e.getMessage(),
                    LossItemResponse.class);
        } finally {
            result = JsonUtils.toJson(response);
            // 日志记录响应json
            if (MobileReturnCodeSupport.CODE_SUCCESS.equals(response.getResultCode())) {
                LogUtils.info("返回结果【" + result + "】");
            } else {
                LogUtils.error("返回结果【" + result + "】");
            }
            result = EncryptUtil.encodeBase64(result);
        }
        return result;
    }

    /**
     * MP16
     * 1.记录用户是否同意此次定损。
     * 2.需要调用理赔接口。
     * 
     * @Title：定损结果确认
     * @author zhangyw 2016年10月12日 下午1:44:34
     * @param data
     * @return String
     */
    @RequestMapping("isAgree.action")
    @ResponseBody
    public String isAgree(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "MP16入参=" + loguuid;
        LossItemRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, LossItemRequest.class);
        LossItemResponse response = new LossItemResponse();
        ReturnMsgDto rmg = new ReturnMsgDto();
        String result = "";
        try {
            String caseNo = request.getCaseNo();
            String caseUuid = request.getCaseUuid();
            String phoneNo = request.getPhoneNo();
            String isAgree = request.getIsAgree();
            String agreeType = request.getAgreeType();
            if ("1".equals(agreeType)) {
                agreeType = "客户确认";
            }
            PrpmCaseInfo prpmCaseInfo = caseInfoService.selectByPrimaryKey(caseUuid);
            List<LossItemDto> lossItemDtoList = request.getLossItemList();
            if (lossItemDtoList != null && lossItemDtoList.size() > 0) {
                for (int i = 0; i < lossItemDtoList.size(); i++) {
                    PrpmLossConfirm prpmLossConfirm = new PrpmLossConfirm();
                    PrpmLossItem prpmLossItem = lossItemService.selectByPrimaryKey(lossItemDtoList.get(i).getLossUuid());
                    if (isAgree == "1" || "1".equals(isAgree)) {
                        prpmLossItem.setLossitemStatus(Constant.LOSS_CLIENTAGREE);
                        prpmLossConfirm.setLossitemStatus(Constant.LOSS_CLIENTAGREE);
                    } else if ((isAgree == "0" || "0".equals(isAgree))) {
                        prpmLossItem.setLossitemStatus(Constant.LOSS_CLIENTREFUSE);
                        prpmLossConfirm.setLossitemStatus(Constant.LOSS_CLIENTREFUSE);
                    }
                    prpmLossConfirm.setCaseNo(caseNo);
                    prpmLossConfirm.setConfirmDate(new Date());
                    prpmLossConfirm.setConfirmType(1);
                    prpmLossConfirm.setMobile(phoneNo);
                    prpmLossConfirm.setConfirmBy(phoneNo);
                    prpmLossConfirm.setCaseInfoId(prpmCaseInfo.getCaseInfoId());
                    /*
                     * 新接口插入表中多了LossUuid字段，该字段信息通过caseUuid、caseNo、phoneNo三个字段对应
                     * 关联prpm_loss_item表中的CaseUuid、CaseNo、Mobile的三个字段。
                     */
                    prpmLossConfirm.setLossItemId(prpmLossItem.getLossItemId());
                    prpmLossConfirm.setLossId(prpmLossItem.getLossId());
                    PrpmLossConfirm prpmLossConfirmQuery = lossConfirmService.selectByLossItemId(prpmLossItem.getLossItemId());
                    if (prpmLossConfirmQuery != null) {
                        lossConfirmService.updateByPrimaryKeySelective(prpmLossConfirm);
                    } else {
                        prpmLossConfirm.setLossConfirmId(UUIDUtils.getUUIDStr());
                        lossConfirmService.insert(prpmLossConfirm);
                    }
                    prpmLossItem.setLossItemId(prpmLossItem.getLossItemId());
                    lossItemService.updateByPrimaryKeySelective(prpmLossItem);
                }
            }
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null,
                    LossItemResponse.class);
        } catch (Exception e) {
            LogUtils.error("获取案件下定损列表失败，", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, e.getMessage(),
                    LossItemResponse.class);
        } finally {
            result = JsonUtils.toJson(response);
            // 日志记录响应json
            if (MobileReturnCodeSupport.CODE_SUCCESS.equals(response.getResultCode())) {
                LogUtils.info("MP16返回结果【" + result + "】");
            } else {
                LogUtils.error("MP16返回结果【" + result + "】");
            }
            result = EncryptUtil.encodeBase64(result);
        }
        return result;
    }

    /**
     * 自助查勘：查询定损单状态
     * MP19
     * 
     * @return
     * @throws Exception
     */
    @RequestMapping("lossStatusConfirm.action")
    @ResponseBody
    public String lossStatusConfirm(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "MP19入参=" + loguuid;
        LossItemRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, LossItemRequest.class);
        LossItemResponse response = null;
        String caseNo = request.getCaseNo();
        String caseUuid = request.getCaseUuid();
        String result = "";
        try {
            if (StringUtils.isNotBlank(caseNo) && StringUtils.isNotBlank(caseUuid)) {
                // PrpmCaseInfo prpmCaseInfo = caseInfoService.getCaseInfo(registNo);
                PrpmCaseInfo prpmCaseInfo = caseInfoService.getCaseInfoByCaseNoAndUuid(caseNo, caseUuid);
                if (prpmCaseInfo != null && !"".equals(prpmCaseInfo)) {
                    String lossStatus = prpmCaseInfo.getCaseStatus() + "";
                    if (lossStatus != null && !"".equals(lossStatus) && !"null".equals(lossStatus)) {
                        response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null,
                                LossItemResponse.class);
                        response.setLossStatus(lossStatus);
                    } else {
                        response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE,
                                MobileReturnCodeSupport.KEY_MOBILE_00034, LossItemResponse.class);
                    }
                } else {
                    response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_FAIL, MobileReturnCodeSupport.SYS_CODE,
                            MobileReturnCodeSupport.KEY_MOBILE_00021, LossItemResponse.class);
                }
            } else {
                LogUtils.info("入参为空");
                response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_FAIL, MobileReturnCodeSupport.SYS_CODE,
                        MobileReturnCodeSupport.KEY_MOBILE_00021, LossItemResponse.class);
            }
        } catch (Exception e) {
            LogUtils.error("获取案件下定损列表失败，", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, e.getMessage(),
                    LossItemResponse.class);
        } finally {
            result = JsonUtils.toJson(response);
            // 日志记录响应json
            if (MobileReturnCodeSupport.CODE_SUCCESS.equals(response.getResultCode())) {
                LogUtils.info("MP19返回结果【" + result + "】");
            } else {
                LogUtils.error("MP19返回结果【" + result + "】");
            }
            result = EncryptUtil.encodeBase64(result);
        }
        return result;
    }

    /**
     * MP67 @Title:
     * C端图片查勘流转接口 @Description:生成拍照查勘处理任务，对应的C端该用户下每一条任务都生成一条拍照查勘处理任务 @param @param
     * data @param @return 设定文件 @return String 返回类型 @throws
     */
    @RequestMapping("/surveyFinish4C.action")
    @ResponseBody
    public String surveyFinish4C(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "MP67入参=" + loguuid;
        String result = null; // 结果
        BaseResponse response = null;
        SurveyFinishRequest sr = JsonUtils.fromJsonWithBase64(logTitle, data, SurveyFinishRequest.class);
        sr.setLogUuid(loguuid);
        try {
            response = mobileBussService.surveyFinish4C(sr);
        } catch (Exception e) {
            LogUtils.error("MP67->logUuid:" + loguuid + "C端图片查勘流转接口口系统异常,错误信息：", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null,
                    BaseResponse.class);
        } finally {
            // 结果封装
            result = JsonUtils.toJson(response);
            LogUtils.info("MP67->logUuid:" + loguuid + "C端图片查勘流转接口返回结果：" + result);
            // 加密返回
            result = EncryptUtil.encodeBase64(result);
        }
        return result;
    }
}
